package vn.org.hcmut.or.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import vn.org.hcmut.or.model.AvailableJob;
import vn.org.hcmut.or.model.AvailableWindow;
import vn.org.hcmut.or.model.FragmentJob;
import vn.org.hcmut.or.model.SchedulingInfo;
import vn.org.hcmut.or.test.ParserTest;

public class ScheduleParser {
	String inputFilePath;
	SchedulingInfo schedulingInfo;
	ParserTest parserTest = new ParserTest();
	public ScheduleParser(String inputFilePath)
	{
		this.inputFilePath = inputFilePath;
	}
	
	public String getInputFilePath() {
		return inputFilePath;
	}

	public void setInputFilePath(String inputFilePath) {
		this.inputFilePath = inputFilePath;
	}

	public SchedulingInfo getSchedulingInfo() {
		return schedulingInfo;
	}

	public void setSchedulingInfo(SchedulingInfo schedulingInfo) {
		this.schedulingInfo = schedulingInfo;
	}

	public ArrayList<String> getContents(File aFile) {
        ArrayList<String> data = new ArrayList<String>();
	    try {

	      BufferedReader input =  new BufferedReader(new FileReader(aFile));
	      try {
	        String line = null; 

	        while (( line = input.readLine()) != null){
	        	data.add(line);
	        }
	      }
	      finally {
	        input.close();
	      }
	    }
	    catch (IOException ex){
	      ex.printStackTrace();
	    }
	    return data;
	  }
	
	public void parse()
	{
		File testFile = new File(inputFilePath);
		ArrayList<String > data = getContents(testFile);
		if(data.size() < 3)
		{
			System.out.println("Lack info");
			return;
		}
		if(schedulingInfo == null)
			schedulingInfo = new SchedulingInfo();
		String jobDescription = data.get(0);
		String [] header = jobDescription.split(" ");
		
		/* set header*/
		schedulingInfo.setNumOfJob(Integer.parseInt(header[0]));
		schedulingInfo.setMinsplit(Integer.parseInt(header[1]));
		
		/* set avaiable window first to get duration*/
		
		String [] listWindow = data.get(2).split(" ");
		System.out.println("Avaiable Window = "+data.get(2));
		ArrayList<AvailableWindow> listAvailableWindow = new ArrayList<AvailableWindow>();
		for(int i=0;i<listWindow.length;i++)
		{
			AvailableWindow availableWindow = new AvailableWindow();
			availableWindow.setId(i);
			if(i==0){
				availableWindow.setTimeStart(0);
				availableWindow.setT_duration(Integer.parseInt(listWindow[i]));

			}
			else
			{
				AvailableWindow preAvailableWindow = listAvailableWindow.get(i-1);
				availableWindow.setTimeStart(preAvailableWindow.getTimeStart()+preAvailableWindow.getT_duration());
				availableWindow.setT_duration(Integer.parseInt(listWindow[i]) - preAvailableWindow.getTimeStart() - preAvailableWindow.getT_duration());
			}
			listAvailableWindow.add(availableWindow);
		}
		AvailableWindow lastAvailableWindow = new AvailableWindow();
		AvailableWindow preAvailableWindow = listAvailableWindow.get(listAvailableWindow.size()-1);
		lastAvailableWindow.setId(listAvailableWindow.size());
		lastAvailableWindow.setTimeStart(preAvailableWindow.getTimeStart()+preAvailableWindow.getT_duration());
		lastAvailableWindow.setT_duration(10000000);
		listAvailableWindow.add(lastAvailableWindow);
		schedulingInfo.setaWindow(listAvailableWindow);
		/*Test Avaiable window*/
	//	parserTest.printAvaiableWindows(schedulingInfo);
		
		/* set job processing time*/
		String []jobProcessingTime = data.get(1).split(" ");
		System.out.println("jobProcessingTime= "+data.get(1));
		if(jobProcessingTime.length < schedulingInfo.getNumOfJob())
		{
			System.out.println("Lack job");
			return;
		}
		ArrayList<AvailableJob> listAvailableJob = new ArrayList<AvailableJob>();
		for(int j = 0;j<jobProcessingTime.length;j++)
		{
			AvailableJob availableJob = new AvailableJob(Integer.parseInt(jobProcessingTime[j]), listWindow.length+1);
			availableJob.setIndexJob(j);
			listAvailableJob.add(availableJob);
		}
		schedulingInfo.setaJob(listAvailableJob);
		
		
		schedulingInfo.buildAavaibleJobHashTable();
		schedulingInfo.buildAavaibleWindowHashTable();
		schedulingInfo.buildSplitJobByHeuristic();

		//test split job
		//parserTest.printJobs(schedulingInfo);
		
		
	}
	public static void main (String... aArguments) throws IOException {
	    ScheduleParser sp = new ScheduleParser("D:/Master/OR/Assignment/Project/OR_ASS/input/input.txt");
	    sp.parse();
	  }
}
